package com.nuc.kl.module.kpoint.action;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.kpoint.entity.Reference;

@Controller("ReferenceAction")
@Scope("prototype")
public class ReferenceAction extends BaseAction<Reference>{
	
	private String ids;// 删除所用多个id
	private String findName;// 模糊查询所用
	private String rows;// 每页显示的记录数
	private String page;// 当前第几页
	private String indexType;
	private String indexValue;
	private String minPeriod;//最小学时
	private String maxPeriod;//最大学时
	private String order;//是否排序
	private String sort;//排序规则
	
	private String referenceTypeId;
	
	/** 列表页面 */
	public String listUI() throws Exception {
		
		return "list";
	}
	
	
	/** 列表 */
	public String listJson() throws Exception {
		int intPage = Integer.parseInt((page == null || page == "0") ? "1": page);
		int number = Integer.parseInt((rows == null || rows == "0") ? "10": rows);
		int start = (intPage - 1) * number;
	
		//与Dao层对应 实现按个别属性的搜索功能
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("name", model.getName());
		m.put("url", model.getUrl());
		m.put("description", model.getDescription());
		m.put("order", order);
		m.put("sort", sort);
		
		models = referenceService.searchPageByIndex(start,number,m);
		String[] s = {"referenceType","knowledges"};
		toBeJson(models, models.size(),s);

		return null;
	}

	/** 添加 */
	public String add() throws Exception {
		model.setReferenceType(referenceTypeService.getById(Long.parseLong(referenceTypeId)));
		referenceService.save(model);
		
		String str = "{\"status\":\"ok\" , \"message\":\"操作成功!\"}";
		 response.setCharacterEncoding("utf-8");//指定为utf-8  
		response.getWriter().write(str);

		
		return null;
	}

	
	/** 删除 */
	public String deleteByIds() throws Exception {
		
		try {
			referenceService.deleteByIds(ids);
			String str = "{\"status\":\"ok\" , \"message\":\"操作成功!\"}";
			 response.setCharacterEncoding("utf-8");//指定为utf-8  
			response.getWriter().write(str);
		} catch (Exception e) {
			String str = "{\"status\":\"error\" , \"message\":\"操作失败，请重试!\"}";
			 response.setCharacterEncoding("utf-8");//指定为utf-8  
			response.getWriter().write(str);
			e.printStackTrace();
		}
		
		
		return null;
	}

	/** 修改 */
public String edit() throws Exception {
		try {
			referenceService.update(model);
			String str = "{\"status\":\"ok\" , \"message\":\"操作成功!\"}";
			 response.setCharacterEncoding("utf-8");//指定为utf-8  
			response.getWriter().write(str);
		} catch (Exception e) {
			String str = "{\"status\":\"error\" , \"message\":\"操作失败，请重试!\"}";
			 response.setCharacterEncoding("utf-8");//指定为utf-8  
			response.getWriter().write(str);
			e.printStackTrace();
		}
		
		return null;
	}


//	-----------------getter-----------setter---------------------------

	public String getIds() {
		return ids;
	}


	public void setIds(String ids) {
		this.ids = ids;
	}


	public String getFindName() {
		return findName;
	}


	public void setFindName(String findName) {
		this.findName = findName;
	}


	public String getRows() {
		return rows;
	}


	public void setRows(String rows) {
		this.rows = rows;
	}


	public String getPage() {
		return page;
	}


	public void setPage(String page) {
		this.page = page;
	}


	public String getIndexType() {
		return indexType;
	}


	public void setIndexType(String indexType) {
		this.indexType = indexType;
	}


	public String getIndexValue() {
		return indexValue;
	}


	public void setIndexValue(String indexValue) {
		this.indexValue = indexValue;
	}


	public String getMinPeriod() {
		return minPeriod;
	}


	public void setMinPeriod(String minPeriod) {
		this.minPeriod = minPeriod;
	}


	public String getMaxPeriod() {
		return maxPeriod;
	}


	public void setMaxPeriod(String maxPeriod) {
		this.maxPeriod = maxPeriod;
	}


	public String getOrder() {
		return order;
	}


	public void setOrder(String order) {
		this.order = order;
	}


	public String getSort() {
		return sort;
	}


	public void setSort(String sort) {
		this.sort = sort;
	}


	public String getReferenceTypeId() {
		return referenceTypeId;
	}


	public void setReferenceTypeId(String referenceTypeId) {
		this.referenceTypeId = referenceTypeId;
	}


	

}
